/**
 * 
 */
package com.gr.staffpm.pages.dashboard;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;


/**
 * @author Graham Rhodes 12 Mar 2011 15:38:09
 */
public class GadgetContainer extends Panel {

    private static final long serialVersionUID = 1L;
    private static final double INITIAL_WIDTH_OFFSET = 2;
    private static final double WIDTH_OFFSET = 1.147915;
    private static final double WIDTH = 31;
    private static final double INITIAL_HEIGHT_OFFSET = 44.4833;
    private static final double HEIGHT_OFFSET = 16.3997;

    private final List<Gadget> leftGadgets = new ArrayList<Gadget>();
    private final List<Gadget> centerGadgets = new ArrayList<Gadget>();
    private final List<Gadget> rightGadgets = new ArrayList<Gadget>();

    private final RepeatingView gadgets = new RepeatingView("gadgets");

    public enum ALIGNMENT {
        LEFT, CENTER, RIGHT;
    }

    public GadgetContainer(String id) {
        super(id);
    }

    public void addGadget(String label, Component content, double height, ALIGNMENT alignment) {
        switch (alignment) {
            case LEFT:
                addLeftGadget(label, content, height);
                break;
            case CENTER:
                addCenterGadget(label, content, height);
                break;
            case RIGHT:
                addRightGadget(label, content, height);
                break;
        }
        addOrReplace(gadgets);
    }

    private void addLeftGadget(String label, Component content, final double height) {
        final int rowIndex = leftGadgets.size();
        Gadget gadget = new Gadget(String.valueOf(gadgets.size()), label, content) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                super.onComponentTag(tag);
                String style = "left: " + INITIAL_WIDTH_OFFSET + "%;";
                double top = rowIndex == 0 ? INITIAL_HEIGHT_OFFSET : ((INITIAL_HEIGHT_OFFSET + leftGadgets.get(rowIndex - 1).getHeight() + HEIGHT_OFFSET));
                style += " top: " + top + "px; width: " + WIDTH + "%; height: " + height + "px;";
                tag.getAttributes().put("style", style);
                tag.getAttributes().put("class", "gadget color1");
                this.setHeight(rowIndex == 0 ? height : (leftGadgets.get(rowIndex - 1).getHeight() + HEIGHT_OFFSET + height));
            }
        };
        leftGadgets.add(gadget);
        gadgets.add(gadget);
    }

    private void addCenterGadget(String label, Component content, final double height) {
        final int rowIndex = centerGadgets.size();
        Gadget gadget = new Gadget(String.valueOf(gadgets.size()), label, content) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                super.onComponentTag(tag);
                String style = "left: " + (INITIAL_WIDTH_OFFSET + WIDTH + WIDTH_OFFSET) + "%;";
                double top = rowIndex == 0 ? INITIAL_HEIGHT_OFFSET : ((INITIAL_HEIGHT_OFFSET + centerGadgets.get(rowIndex - 1).getHeight() + HEIGHT_OFFSET));
                style += " top: " + top + "px; width: " + WIDTH + "%; height: " + height + "px;";
                tag.getAttributes().put("style", style);
                tag.getAttributes().put("class", "gadget color1");
                this.setHeight(rowIndex == 0 ? height : (centerGadgets.get(rowIndex - 1).getHeight() + HEIGHT_OFFSET + height));
            }
        };
        centerGadgets.add(gadget);
        gadgets.add(gadget);
    }

    private void addRightGadget(String label, Component content, final double height) {
        final int rowIndex = rightGadgets.size();
        Gadget gadget = new Gadget(String.valueOf(gadgets.size()), label, content) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                super.onComponentTag(tag);
                String style = "left: " + (INITIAL_WIDTH_OFFSET + (WIDTH + WIDTH_OFFSET) * 2) + "%;";
                double top = rowIndex == 0 ? INITIAL_HEIGHT_OFFSET : ((INITIAL_HEIGHT_OFFSET + rightGadgets.get(rowIndex - 1).getHeight() + HEIGHT_OFFSET));
                style += " top: " + top + "px; width: " + WIDTH + "%; height: " + height + "px;";
                tag.getAttributes().put("style", style);
                tag.getAttributes().put("class", "gadget color1");
                this.setHeight(rowIndex == 0 ? height : (rightGadgets.get(rowIndex - 1).getHeight() + HEIGHT_OFFSET + height));
            }
        };

        rightGadgets.add(gadget);
        gadgets.add(gadget);
    }

}
